package cn.tedu.charging.order.repository;

import cn.tedu.charging.common.pojo.po.ChargingBillExceptionPO;
import cn.tedu.charging.common.pojo.po.ChargingBillSuccessPO;
import cn.tedu.charging.order.mapper.SuccessBillMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public class SuccessBillRepo {
    @Autowired
    private SuccessBillMapper successBillMapper;

    public Long countByBillId(String billId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bill_id", billId);
        return successBillMapper.selectCount(queryWrapper);
    }

    public void save(ChargingBillSuccessPO po) {
        successBillMapper.insert(po);
    }

    public ChargingBillSuccessPO findByBillId(String billId) {
        // select * from charging_bill_success where bill_id = ?
        QueryWrapper<ChargingBillSuccessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bill_id", billId);
        return successBillMapper.selectOne(queryWrapper);
    }

    public void updateStatus(String billId, int i) {
        // update charging_bill_success set bill_status = ? where bill_id = ?
        UpdateWrapper<ChargingBillSuccessPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bill_id", billId);
        updateWrapper.set("bill_status", i);
        updateWrapper.set("update_time", new Date());
        successBillMapper.update(null, updateWrapper);
    }
}
